import { SET_USER_TOKEN, SET_USER } from '@/constants/mutation-types'
import * as API from '@/constants/api'
import { tokenStorage } from '@/utils/storage'

export const state = () => ({
  loggedIn: false,
  userToken: '',
  user: {}
})
export const mutations = {
  [SET_USER_TOKEN] (state, userToken) {
    state.userToken = userToken
  },
  [SET_USER] (state, user) {
    if (user) {
      state.loggedIn = true
    } else {
      state.loggedIn = false
    }
    state.user = user
  }
}
export const actions = {
  async login ({ commit }, params) {
    const type = params[1]
    const param = params[0]
    let payload
    if (type === 1) {
      // 邮箱登录
      payload = await this.$axios.$post(API.EMAIL_LOGIN, param)
    } else if (type === 3) {
      payload = await this.$axios.$post(API.SECOND_LOGIN, param)
    } else {
      // 手机号登陆
      payload = await this.$axios.$post(API.PHONE_LOGIN, param)
    }
    const accessToken = payload.data.access_token
    tokenStorage.set(accessToken)
    commit(SET_USER_TOKEN, accessToken)
  },
  logout ({ commit }) {
    tokenStorage.remove()
    commit('SET_USER', '')
    commit('SET_USER_TOKEN', '')
  },
  async getUserInfo ({ commit }, params) {
    const { data } = await this.$axios.$post(API.GET_USER_INFO, params)
    commit(SET_USER, data)
  },
  async sendSms ({ commit }, params) {
    return await this.$axios.$post(API.SEND_SMS, params)
  },
  async phoneSign ({ commit }, params) {
    return await this.$axios.$post(API.PHONE_SIGN, params)
  },
  async sendEmail ({ commit }, params) {
    return await this.$axios.$post(API.EMAIL_CODE, params)
  },
  async emailSign ({ commit }, params) {
    return await this.$axios.$post(API.EMAIL_SIGN, params)
  },
  async editUserInfo ({ commit }, params) {
    return await this.$axios.$post(API.EDIT_USER_INO, params)
  },
  // 认证
  async uploadPhoto ({ commit }, params) {
    return await this.$axios.$post(API.UPLOAD_PHOTO, params)
  },
  async uploadCapture ({ commit }, params) {
    return await this.$axios.$post(API.UPLOAD_CAPTURE, params)
  },
  async uploadVideo ({ commit }, params) {
    return await this.$axios.$post(API.UPLOAD_VIDEO, params)
  },
  async authPerson ({ commit }, params) {
    return await this.$axios.$post(API.AUTH_PERSON, params)
  },
  async authMesiness ({ commit }, params) {
    return await this.$axios.$post(API.AUTH_MESINESS, params)
  },
  async authEliminate ({ commit }, params) {
    return await this.$axios.$post(API.ELIMINATE_AUTH, params)
  },
  // 安全设置
  async updatePassword ({ commit }, params) {
    return await this.$axios.$post(API.UPDATE_PASSWORD, params)
  },
  async sendEmailCode ({ commit }, params) {
    return await this.$axios.$post(API.SEND_EMAIL_CODE, params)
  },
  async bindEmail ({ commit }, params) {
    return await this.$axios.$post(API.BIND_EMAIL, params)
  },
  async bindPhone ({ commit }, params) {
    return await this.$axios.$post(API.BIND_PHONE, params)
  },
  async forgetPassword ({ commit }, params) {
    return await this.$axios.$post(API.FORGET_PASSWORD, params)
  },
  async forgetPasswordByEmail ({ commit }, params) {
    return await this.$axios.$post(API.EMAIL_FORGET_PASSWORD, params)
  }
}
